<?php
/* 签到抽奖类 */
namespace Api\Controller;
use Think\Controller;
use Think\Log;
class LotteryController extends Controller {

	protected $lottery; //签到活动
	protected $nextSignSecond = 0; //下次签到秒数
	protected $userId = 0;  //用户ID
	protected $role = 0;   //角色 1业主2师傅
 	
 	protected $start = '';
 	protected $end = '';
	public function __construct()
	{
		//解决跨域
		echoHeader();
	    $this->role = I('post.role','','intval');     
        $this->userId = I('post.user_id',0, 'intval');  
        $this->lottery = D('Lottery');
        if ($this->userId <= 0) {
        	$this->ajaxReturn(array('err_code'=>20001, 'err_msg'=>'用户不能为空'), 'JSON');
        }
        $map['user_id'] = $this->userId;
        $user=M('user')->where($map)->find();
        if (empty($user)) {
        	$this->ajaxReturn(array('err_code'=>20002,'err_msg'=>'用户不存在'),'JSON');
        }elseif($user['identity'] != $this->role){
        	$this->ajaxReturn(array('err_code'=>20003,'err_msg'=>'用户登录角色错误'),'JSON');
        }

        if(ACTION_NAME != 'status'){
	        //检查当天是否有签到活动
	        if (!$lottery = $this->lottery->checkLottery($this->role)) {
	        	$this->ajaxReturn($this->lottery->getError(), 'JSON');
	        }
	        //var_dump($lottery);exit;
	        //返回活动每日开始结束时间
	        if ($this->role == 1) {
	        	$this->start = '00:00';
	        	$this->end   = '24:00';
	        }else{
		        $this->start = $lottery['start_hour'] >= 10 ? $lottery['start_hour'].':00' : '0'.$lottery['start_hour'].':00';
				$this->end = $lottery['end_hour'] >= 10 ? $lottery['end_hour'].':00' : '0'.$lottery['end_hour'].':00';
	        	
	        }
	        //检查用户是否已签到
	        $check = $this->lottery->checkSignIn($this->userId, $this->role);
			if($check['sign'] === false){
				$ret = $this->lottery->getError();
				$ret['start_time'] = $this->start;
				$ret['end_time'] = $this->end;
				$ret['second'] = $check['second'];
		        if ($this->role == 2) {//师傅返回下一次签到的时间
		         	$this->nextSignSecond = $check['second'];
		        }
	        	$this->ajaxReturn($ret,'JSON');
			}

        }
	}

	//设置用户签到意愿状态
	public function status()
	{
		if ($this->role == 2) {//师傅端不用记录，直接返回
			$this->ajaxReturn(array('err_code'=>200,'err_msg'=>0,'second'=>0), 'JSON');
		}
		 //计算当天剩余秒数
        $expire = strtotime(date('Y-m-d')) + 86400 - time();
        $key = '_LOTTERY_STATUS_'.$this->userId.'_';
        S(array('type'=>'redis', 'expire'=>$expire));
        S($key, 1);
		$this->ajaxReturn(array('err_code'=>200,'err_msg'=>1,'second'=>$expire), 'JSON');
	}


    //签到首页
    public function index(){
    	if($this->role == 1){ //业主判断是否不想签到
        	$key = '_LOTTERY_STATUS_'.$this->userId.'_';
        	S(array('type'=>'redis'));
        	$status = S($key);
			if ($status) {
				//计算当天剩余秒数
				$expire = strtotime(date('Y-m-d')) + 86400 - time();
				$this->ajaxReturn(array('err_code'=>10007,'err_msg'=>'业主不想签到','second'=>$expire), 'JSON');
			}
        }

  //   	if ($this->nextSignSecond > 0) {
		// 	$ret = array('err_code'=>10005, 
		// 		         'err_msg'=>'当场次您已签到，请等待下场次开始',
		// 		         'second'=>$this->nextSignSecond);
		// 	$this->ajaxReturn($ret, 'JSON');
		// }
		//获取对应转盘信息
		$prizeList = $this->lottery->getPrizeList($this->role);
		if($prizeList){
			$data[] = array('id'=>'0','name'=>'谢谢参与','amount'=>'0.00','volume_id'=>'0');
			foreach ($prizeList as $k => $v) {
				unset($v['surplus']);
				$data[] = $v;
				if (count($data) >= 8) {
					break;
				}
			}
			$ret['second']  = $this->nextSignSecond;
			$ret['err_code'] = 200;
			$ret['err_msg']  = $data;
		}else{
			$ret = $this->lottery->getError();
		}		
		
		$ret['start_time'] = $this->start;
		$ret['end_time'] = $this->end;
		$this->ajaxReturn($ret, 'JSON');
	}

	//签到操作
	public function signIn()
	{
		// if ($this->nextSignSecond > 0) {
		// 	$ret = array('err_code'=>10005, 
		// 		         'err_msg'=>'当场次您已签到，请等待下场次开始',
		// 		         'second'=>$this->nextSignSecond);
		// 	$this->ajaxReturn($ret, 'JSON');
		// }
		$prize = $this->lottery->doLottery($this->userId, $this->role);

		if ($prize) {
			//$ret['data'] = $prize['prize'];
			$ret['err_code'] = 200;
			$ret['err_msg'] = $prize['prize'];
			$ret['second'] = $prize['second'];
		}else{
			$ret = $this->lottery->getError();
		}
		$this->ajaxReturn($ret, 'JSON');
	}
}